package DAL;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import oracle.jdbc.OraclePreparedStatement;

public class PersonneDAL extends Dal {

	public void insertionPersonne(int id, String nom, String anniv,
			String lieu, String photo, String bio) {
		CallableStatement pstmt = null;
		String[] tablename = new String[3];
		tablename[0] = "acteur";
		tablename[1] = "realisateur";
		tablename[2] = "scenariste";
		String[] nomPrenom = retrievePrenomNom(nom);
		for (int i = 0; i < 3; i++) {

			try {
				int paramCount = 3;
				String values = "INSERT INTO " + tablename[i]
						+ " (id, prenom, nom";
				String params = "VALUES(?, ?, ?";

				if (anniv != null && anniv.length() > 0) {
					values += ", dateNaissance ";
					params += ", ?";
				}
				if (lieu != null && lieu.length() > 0) {
					values += ", LIEUNAISSANCE";
					params += ", ?";
				}
				if (photo != null && photo.length() > 0) {
					values += ", PHOTO";
					params += ", ?";
				}
				if (bio != null && bio.length() > 0) {
					values += ", BIOGRAPHIE";
					params += ", ?";
				}
				values += ")";
				params += ")";

				pstmt = Dal.connection.prepareCall(values + params);
				pstmt.setInt(1, id);
				pstmt.setString(2, nomPrenom[0]);
				if (nomPrenom.length > 1)
					pstmt.setString(3, nomPrenom[1]);
				else
					pstmt.setString(3, nomPrenom[0]);

				if (anniv != null && anniv.length() > 0) {
					DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
					Date setDate = (Date) df.parse(anniv);
					long dateTime = setDate.getTime();
					java.sql.Date sqlDate = new java.sql.Date(dateTime);
					pstmt.setDate(++paramCount, sqlDate);
				}
				if (lieu != null && lieu.length() > 0) {
					pstmt.setString(++paramCount, lieu);
				}
				if (photo != null && photo.length() > 0) {
					pstmt.setString(++paramCount, photo);
				}
				if (bio != null && bio.length() > 0) {

					((OraclePreparedStatement) pstmt).setStringForClob(
							++paramCount, bio);
				}

				System.out.println("Insertion de la personne:" + id);
				pstmt.executeUpdate();

			} catch (SQLException e) {
				e.printStackTrace();
			} catch (ParseException e) {
				// la date d'anniversaire est chier :|
				e.printStackTrace();
			} finally {
				try {
					pstmt.close();
				} catch (SQLException e) {
					System.out.println("HAPPEN ON THE CLOSE");
					e.printStackTrace();
				}
			}
		}
	}
	 
	private String[] retrievePrenomNom(String arg){ 
	        String[] result = {"", ""}; 
	          
	        if(arg != null && arg.length() > 0){ 
	            String[] tmp = arg.split(" "); 
	            int tmpLen = tmp.length; 
	            if(tmpLen > 1){ 
	                String prenom = ""; 
	                for(int i = 0; i < (tmpLen -1); i++){ 
	                    prenom += tmp[i]; 
	                } 
	                String nom = tmp[tmpLen -1]; 
	                  
	                result[0] = prenom; 
	                result[1] = nom; 
	            }
	            else{
	            	result[0]= arg;
	            	result[1]= arg;
	            }
	        } 
	          
	        return result; 
	    } 
	
}
